package com.yantong.controller;

import com.yantong.annotation.Log;
import com.yantong.constant.BusinessCode;
import com.yantong.utils.Tree;
import com.yantong.result.ResultVo;
import com.yantong.pojo.Permission;
import com.yantong.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kanghaixin
 * @date 2018/6/27.15:45
 */
@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController{
    private static final String prefix = "system/menu";
//    private static final String prefix = "";
    @Autowired
    private MenuService menuService;

    /**
     * 进入权限管理界面
     * @param model
     * @return
     */
    @RequiresPermissions("sys:menu:menu")
    @GetMapping()
    public String menu(Model model) {
        return prefix+"/menu";
    }

    /**
     * 查看权限列表
     * @return
     */
    @RequiresPermissions("sys:menu:menu")
    @RequestMapping("/list")
    @ResponseBody
    public List<Permission> list() {
        return menuService.listMenus(getUserFlag());
    }

    /**
     * 进入权限添加页面
     * @param model
     * @param parentId
     * @return
     */
    @RequiresPermissions("sys:menu:add")
    @GetMapping("/add/{parentId}")
    public String add(Model model, @PathVariable("parentId") Long parentId) {
        model.addAttribute("parentId", parentId);
        if (parentId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.getPermById(parentId).getName());
        }
        return prefix + "/add";
    }

    /**
     * 进入权限编辑页面
     * @param model
     * @param permId
     * @return
     */
    @RequiresPermissions("sys:menu:edit")
    @GetMapping("/edit/{permId}")
    public String edit(Model model, @PathVariable("permId") Long permId) {
        Permission mdo = menuService.getPermById(permId);
        Long parentId = mdo.getParentId();
        model.addAttribute("parentId", parentId);
        if (parentId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.getPermById(parentId).getName());
        }
        model.addAttribute("menu", mdo);
        return prefix+"/edit";
    }

    /**
     * 添加权限菜单
     * @param menu
     * @return
     */
    @Log(module = "菜单管理", operation = "新增菜单")
    @RequiresPermissions("sys:menu:add")
    @PostMapping("/save")
    @ResponseBody
    public ResultVo save(Permission menu) {
        if (menuService.savePerm(menu) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.PERM_SAVE_ERROR);
        }
    }

    /**
     * 更新权限菜单
     * @param menu
     * @return
     */
    @Log(module = "菜单管理", operation = "编辑菜单")
    @RequiresPermissions("sys:menu:edit")
    @PostMapping("/update")
    @ResponseBody
    public ResultVo update(Permission menu) {
        if (menuService.updatePerm(menu) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.PERM_UPDATE_ERROR);
        }
    }

    /**
     * 删除权限菜单
     * @param id
     * @return
     */
    @Log(module = "菜单管理", operation = "删除菜单")
    @RequiresPermissions("sys:menu:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResultVo remove(Long id) {
        if (menuService.removePerm(id) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.PERM_DELETE_ERROR);
        }
    }

    /**
     * 所有权限的树形结构信息
     * @return
     */
    @GetMapping("/tree")
    @ResponseBody
    public Tree<Permission> tree() {
        return menuService.getPermTree(getUserFlag());
    }

    /**
     * 根据角色获取角色下权限的树形结构
     * @param roleId
     * @return
     */
    @GetMapping("/tree/{roleId}")
    @ResponseBody
    public Tree<Permission> tree(@PathVariable("roleId") Long roleId) {
        return menuService.getTreeByRoleId(roleId, getUserFlag());
    }
}
